package com.ly10111.customedittext.ime.dict.utils;

import android.content.Context;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileUtil {

    private static void copyDb(Context context, InputStream in, String dbName) {
        File wordDb = new File(String.format("/data/data/%s/databases/%s", context.getPackageName(), dbName));
        if (!wordDb.exists()) {
            if (!wordDb.getParentFile().exists()) wordDb.getParentFile().mkdirs();
            try {
                wordDb.createNewFile();
                FileOutputStream fos = new FileOutputStream(wordDb);
                byte[] bytes = new byte[1024];
                int len;
                while ((len = in.read(bytes)) > 0) {
                    fos.write(bytes, 0, len);
                }
                fos.flush();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void copyDb(Context context) {
        try {
            InputStream dict = context.getAssets().open("dict.db0");
            copyDb(context, dict, "dict.db0");
            dict.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
